<!--
* description: 采购管理-订单手工分配
* author: hzm
* createdDate: 2021-1-21
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="dynamicApiConfigs"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :callback-obj="callbackObj"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-fix-choose="false"
      :dynamic-is-fix-index="false"
      :dynamic-api-style="'restful'"
    />
    <detailList
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :dynamic-api-style="'restful'"
      @close="close"
    />
  </div>
</template>

<script>
// import { formMixins } from "@/components/mixins/formMixins";
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import { orgApis } from '@/api/paSparePartsManagement'
import { exportExcel } from '@/utils/getTimeString.js'

export default {
  name: 'OrderReview',
  components: {
    OneTableTemplate,
    DetailList: () => import('./detailList')
  },
  mixins: [oneTableWithViewTemplateMixins],
  data() {
    return {
      selectiondata: [],
      editRowData: {},
      dynamicApiConfigs: orgApis.queryDataAll,
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn',
              label: '操作', // 操作
              codeField: 'controlBtn',
              width: 100,
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '查看明细', // 编辑
                  align: 'center',
                  codeField: 'editControlBtn',
                  clickEvent: (index) => {
                    this.edit(index)
                  },
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'dlrCode',
              label: '网点编码',
              align: 'center'
            },
            {
              prop: 'dlrShortName',
              label: '网点名称',
              align: 'center'
            },
            {
              prop: 'purOrderCode',
              label: '订单号',
              align: 'center'
            },
            {
              prop: 'purOrderTypeName',
              label: '订单类型',
              align: 'center'
            },
            {
              prop: 'purDate',
              label: '订货日期',
              align: 'center'
            },
            {
              prop: 'orderVariety',
              label: '订单品种数',
              align: 'center'
            },
            {
              prop: 'purAmount',
              label: '订货金额',
              align: 'center'
            },
            // {
            //   prop: 'transportModeName',
            //   label: '运输方式',
            //   align: 'center'
            // },
            {
              prop: 'rejectReason',
              label: '驳回原因+',
              isComponent: true,
              align: 'center',
              width: 150,
              comps: [
                {
                  compKey: 'rejectReason',
                  codeField: 'rejectReason',
                  component: () => import('@/components/org/commonInput')
                }
              ]
            },
            {
              prop: 'remark',
              label: '备注+',
              isComponent: true,
              align: 'center',
              width: 150,
              comps: [
                {
                  compKey: 'remark',
                  codeField: 'remark',
                  component: () => import('@/components/org/commonInput')
                }
              ]
            }
          ],
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '网点：',
              codeField: 'dlrId',
              lookuptype: 'VE0212',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 6
            },
            {
              compKey: 'compKey2',
              labelName: '订单号：',
              codeField: 'purOrderCode',
              lookuptype: 'VE0212',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 6
            },
            // {
            //   compKey: "compKey3",
            //   labelName: "备件号：",
            //   codeField: "hostQualityState",
            //   lookuptype: "VE0212",
            //   component: () => import("@/components/org/commonInput"),
            //   type: "inputText",
            //   isMust: true,
            //   span: 6,
            // },
            {
              compKey: 'compKey4',
              labelName: '订货日期：',
              codeField: 'beginDate,endDate',
              component: () => import('@/components/org/datePicker/andtDatePicker'),
              type: 'YYYY-MM-DD',
              // dataToObject:{timer:true,thirtyTime:true,hhmmss: true},
              format: 'yyyy-MM-dd',
              dateOptionsType: '0',
              isMul: false,
              isMust: true,
              span: 6
            },
            {
              compKey: 'compKey5',
              labelName: '订单类型：',
              codeField: 'purOrderType',
              textField: 'partRepairTypeName',
              customObj: {
                api: orgApis.queryDropDownList
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: true,
              span: 6
            }
          ],
      tableButtons: [
        {
          conmpKey: 'btenKey1',
          type: 'primary',
          size: 'small',
          text: '查询',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          position: 'right'
        },
        {
          conmpKey: 'btenKey2',
          type: '',
          size: 'small',
          text: '手工分配',
          clickEvent: () => this.getReject(2),
          position: 'right'
        },
        {
          conmpKey: 'btenKey3',
          type: '',
          size: 'small',
          text: '驳回',
          clickEvent: () => this.getReject(3),
          position: 'right'
        },
        {
          conmpKey: 'btenKey4',
          type: '',
          size: 'small',
          clickEvent: () => this.export(),
          text: this.$t('sys.button.export'),
          name: 'export',
          position: 'right' // 导出
        },
        {
          conmpKey: 'btenKey5',
          type: '',
          size: 'small',
          text: '重置',
          name: 'reset',
          clickEvent: () => this.reset(),
          position: 'right'
        }
      ],
      formField: this.$utils.getFormField(this, {
        dlrId: '',
        purOrderCode: '',
        endDate: '',
        beginDate: '',
        purOrderType: ''
      }),
      callbackObj: {
        // 手动勾选网格时，拿到勾选网格的数据
        onCheckSelect: (selection, row) => {
          this.selectiondata = selection
        }
      }
    }
  },
  mounted() {
    this.queryTable(1)
    // this.$refs.multipleTable.list = [
    //   {'remark':'1','rejectReason':'2'}
    // ]
  },
  methods: {
    close(type) {
      this.editPopupsVisible = type
    },
    // 编辑获取数据
    edit(index) {
      this.editPopupsState = 'edit'
      this.editPopupsVisible = true
      this.editRowData = this.$utils.deepCopy(this.$refs['multipleTable'].list[index])
    },
    export() {
      exportExcel(this.formField, orgApis.queryExportDetails.APIUrl, orgApis.queryExportDetails.ServiceCode, '订单手工分配')
    },
    getReject(num) {
      if (!this.selectiondata.length) return this.$message('请选择要操作的数据!')
      let isValid = true
      if (num == 3) {
        this.selectiondata.every(row => {
          if (!row['rejectReason']) {
            isValid = false
            this.$message('请填写驳回原因！')
            return false
          }
          return true
        })
      }
      if (!isValid) return
      const that = this
      const queryObj = {
        apiConfig: orgApis.getTurnDown,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: num == 2 ? { list: that.selectiondata } : that.selectiondata
        }
      }
      if (num == 2) {
        queryObj.apiConfig = orgApis.manualAssignment
      } else if (num == 3) {
        queryObj.apiConfig = orgApis.getTurnDown
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then((response) => {
        if (response.result === '0') {
          return this.$message(response.msg || '分配失败!')
        }
        var getResult = response
        if (getResult.result === '1') {
          that.$message({
            message: getResult.msg,
            type: 'success',
            duration: 2000
          })
          that.queryTable(1)
          this.selectiondata = []
        } else {
          that.$message({
            message: getResult.msg,
            type: 'warning',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

<style>
</style>
